package com.gxuwz.business.dao.impl;

import org.springframework.stereotype.Repository;

import com.gxuwz.business.entity.Medicine;
import com.gxuwz.business.entity.Standard;
import com.gxuwz.business.entity.Student;
import com.gxuwz.util.Pager;

@Repository("standardDAO")
public class StandardDAO extends BaseDaoImpl<StandardDAO>{

	/**
	 * 分页查询(条件查询)
	 */
	public Pager<Standard> getByPager(Standard standard, Integer page, Integer limit){
		String hql = "from Standard where 1=1 and status = 0";
		//模糊查询
		if(null != standard.getStandDesc() && !"".equals(standard.getStandDesc())){
			String key = standard.getStandDesc();
			hql += " and (standId like '%"+key+"%' or standName like '%"+key+"%' or standPrice like '%"+key+"%' "
					+ "or standUnit like '%"+key+"%' or standDesc like '%"+key+"%')";
		}
		return super.find(hql, null, null, (page-1)*limit, limit);
	}
	
	/**
	 * 添加标准信息
	 */
	public boolean add(Standard standard){
		try{
			System.out.println(standard);
			getHibernateTemplate().save(standard);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 检测标准格式是否已存在
	 */
	public boolean checkStandName(Standard standard){
		return checkExist(Standard.class, "standName", standard.getStandName());
	}
	
	/**
	 * 伪删除
	 */
	public boolean delete(Standard standard){
		boolean status = false;
		String hql = "update Standard set status = 1 where standId = '"+standard.getStandId()+"'";
		try{
			getHibernateTemplate().bulkUpdate(hql);
			status = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return status;   
	}
	
	/**
	 * 根据治疗费用标准编号查询
	 */
	public Standard findById(Standard standard){
		String hql = "from Standard where standId = '"+standard.getStandId()+"'";
		try {
			return (Standard) getHibernateTemplate().find(hql, null).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 只更新姓名、性别、年龄、年级、学院、班别、医保类型、挂号类型
	 */
	public boolean update(Standard s){
		boolean isSucceed = false;
		String hql = "update Standard set standName=?, standPrice=?, standUnit=?, standDesc=? where standId=?";
		try {
			int count = super.getHibernateTemplate().bulkUpdate(hql, s.getStandName(),s.getStandPrice(),s.getStandUnit(),s.getStandDesc(),s.getStandId());
//			System.out.println("更改了"+count+"条数据");
			isSucceed = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isSucceed;
	}
}
